package com.saceem.imagenes.qr;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Writer;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

// QR Code Generator in Java using opensource library ZXing
public class GenerateQRCode {

	// Image properties
	private static final int qr_image_width = 400;
	private static final int qr_image_height = 400;
	private static final String IMAGE_FORMAT = "png";
	private static final String IMG_PATH = "C:/Users/Public/Pictures/Sample Pictures/BarCodes/firma.png";
	private static final String IMG_PATH_QR = "C:/Users/Public/Pictures/Sample Pictures/QRCodes/firmaQR.png";

	// Let's do it
	public static void main(String[] args) throws Exception {

		// URL to be encoded
		// String data = "http://www.google.com";
		String data = "http://www.saceem.com";
		// String data = "01021234565";

		// Encode URL in QR format
		BitMatrix matrix;
		Writer writer = new QRCodeWriter();

		try {

			matrix = writer.encode(data, BarcodeFormat.QR_CODE, qr_image_width,
					qr_image_height);

			try {
				MultiFormatWriter writer2 = new MultiFormatWriter();
				
				QRCodeWriter qrEncoder =  new QRCodeWriter();
				
				String title = "Marcelo Duhart";
				
				String a = "MECARD:N:Bordenave,Nicolas;TEL:099601500;EMAIL:nborde@gmail.com;URL:http://twitter.com/@nbordenave;BDAY:19800430;";
				
				StringBuffer sb = new StringBuffer();
				
				sb.append("MECARD:");
				sb.append("N:Bordenave,Nicolas;");
//				sb.append("TEL:29160208*141;");
				sb.append("TEL:099601500;");
				sb.append("EMAIL:nborde@gmail.com;");
				sb.append("URL:http://twitter.com/@nbordenave;");
				sb.append("BDAY:19800430;");
				//sb.append("ADR:21 de Setiembre,2682,apto 601,CP 11300, Montevideo, Uruguay;");
				sb.append("ADR:21 de Setiembre,2682/601,Montevideo,Montevideo,11300,Uruguay;");
				
				Hashtable<String, String> hints = new Hashtable<String, String>();
				hints.put("N:", "Nicolas Bordenave");
				hints.put("TEL:", "099601500");
				hints.put("EMAIL:", "nborde@gmail.com");
				hints.put("BDAY:", "19800430");
				hints.put("URL:", "http://twitter.com/@nbordenave;");
				

//				hints.put(EncodeHintType.ERROR_CORRECTION,
//						ErrorCorrectionLevel.Q);

				MatrixToImageWriter.writeToFile(writer2.encode(sb.toString(),
						BarcodeFormat.QR_CODE, qr_image_width, qr_image_height,
						hints), "png", new File(IMG_PATH_QR));
				
//				MatrixToImageWriter.writeToFile(qrEncoder.encode(title,
//						BarcodeFormat.QR_CODE, qr_image_width, qr_image_height,
//						hints), "png", new File(IMG_PATH_QR));

			} catch (Exception e) {
				System.out.println("failure: " + e);
			}

			// Esto es para generar CODE128
			// matrix = writer.encode(data, BarcodeFormat.CODE_128,
			// qr_image_width, qr_image_height);

		} catch (WriterException e) {
			e.printStackTrace(System.err);
			return;
		}

		// Create buffered image to draw to
		BufferedImage image = new BufferedImage(qr_image_width,
				qr_image_height, BufferedImage.TYPE_INT_RGB);

		// Iterate through the matrix and draw the pixels to the image
		for (int y = 0; y < qr_image_height; y++) {
			for (int x = 0; x < qr_image_width; x++) {
				int grayValue = (matrix.get(x, y) ? 1 : 0) & 0xff;
				image.setRGB(x, y, (grayValue == 0 ? 0 : 0xFFFFFF));
			}
		}

		// Write the image to a file
		FileOutputStream qrCode = new FileOutputStream(IMG_PATH);
		ImageIO.write(image, IMAGE_FORMAT, qrCode);

		qrCode.close();

	}

}
